﻿
/*
传入fileModel对象
*/
var initCalculate = function (fileModel) {

    "use strict";
    var file_id = 1, drop_zone;
    window.dicHash = new Array();
    var fileCount = 0;
    var idStr="#"+fileModel.btnID;
    jQuery(idStr).click(function () {
        document.getElementById(fileModel.fileID).addEventListener('change', handle_file_select, false);
        document.getElementById(fileModel.fileID).click();
        return true;
    });
    //document.getElementById(fileModel.btnID).onclick = function () {
       
    //    document.getElementById(fileModel.fileID).addEventListener('change', handle_file_select, false);
    //    document.getElementById(fileModel.fileID).click();
    //    return true;
    //};

    if ((typeof File !== 'undefined') && !File.prototype.slice) {
        if (File.prototype.webkitSlice) {
            File.prototype.slice = File.prototype.webkitSlice;
        }

        if (File.prototype.mozSlice) {
            File.prototype.slice = File.prototype.mozSlice;
        }
    }

    if (!window.File || !window.FileReader || !window.FileList || !window.Blob || !File.prototype.slice) {
        alert('File APIs are not fully supported in this browser. Please use latest Mozilla Firefox or Google Chrome.');
    }

    function hash_file(file, workers) {
        var i, buffer_size, block, threads, reader, blob, handle_hash_block, handle_load_block;

        handle_load_block = function (event) {
            for (i = 0; i < workers.length; i += 1) {
                threads += 1;
                workers[i].postMessage({
                    'message': event.target.result,
                    'block': block
                });
            }
        };
        handle_hash_block = function (event) {
            threads -= 1;

            if (threads === 0) {
                if (block.end !== file.size) {
                    block.start += buffer_size;
                    block.end += buffer_size;

                    if (block.end > file.size) {
                        block.end = file.size;
                    }
                    reader = new FileReader();
                    reader.onload = handle_load_block;
                    blob = file.slice(block.start, block.end);

                    reader.readAsArrayBuffer(blob);
                }
            }
        };
        buffer_size = 64 * 16 * 1024;
        block = {
            'file_size': file.size,
            'start': 0
        };

        block.end = buffer_size > file.size ? file.size : buffer_size;
        threads = 0;

        for (i = 0; i < workers.length; i += 1) {
            workers[i].addEventListener('message', handle_hash_block);
        }
        reader = new FileReader();
        reader.onload = handle_load_block;
        blob = file.slice(block.start, block.end);

        reader.readAsArrayBuffer(blob);
    }

    function handle_worker_event(name, id) {
        //event.stopPropagation();
        //event.preventDefault();
        //var files = event.dataTransfer ? event.dataTransfer.files : event.target.files;
        return function (event) {
            if (event.data.result) {
                jQuery("#sha1_file_hash_" + id).parent().html(event.data.result);
                window.dicHash[name] = event.data.result;
                //files[id]["sha1"]=event.data.result;
                var len = 0;
                for (var i in window.dicHash) {
                    len++;
                }

                if (len == fileCount) {
                    //隐藏hash容器
                    //jQuery("#list").hide();
                    ////显示文件上传容器
                    //jQuery("#" + fileModel.filelistholder).show();
                    //jQuery("#btnUploadAll").attr("disabled", false);
                }
            } else {
                jQuery("#sha1_file_hash_" + id + ' .bar').css('width', event.data.block.end * 100 / event.data.block.file_size + '%');
            }
        };
    }

    function handle_file_select(event) {
        event.stopPropagation();
        event.preventDefault();

        var i, output, files, file, workers, worker;
        files = event.dataTransfer ? event.dataTransfer.files : event.target.files;
        output = [];

        //记录文件总数
        fileCount += files.length;

        for (i = 0; i < files.length; i += 1) {
            file = files[i];
            workers = [];

            output.push('<tr><td class="span12"><strong>', file.name, '</strong></td><td> (', file.type || 'n/a', ') - ', file.size, ' bytes</td></tr>');

            //计算sha1
            output.push('<tr>', '<td>SHA1</td><td> <div class="progress progress-striped active" style="margin-bottom: 0px" id="sha1_file_hash_', file_id, '"><div class="bar" style="width: 0%;"></div></div></td></tr>');
            worker = new Worker('/Scripts/hash/sha1.js');
            worker.addEventListener('message', handle_worker_event(file.name, file_id));
            workers.push(worker);

            hash_file(file, workers);
            file_id += 1;

        }

        //document.getElementById('list').innerHTML = '<table class="table table-striped table-hover">' + output.join('') + '</table>' + document.getElementById('list').innerHTML;
    }

    function handle_drag_over(event) {
        event.stopPropagation();
        event.preventDefault();
    }

    drop_zone = $(idStr);// document.getElementById(fileModel.btnID);

    //drop_zone.addEventListener('dragover', handle_drag_over, false);
    //drop_zone.addEventListener('drop', handle_file_select, false);

    //document.getElementById(fileModel.fileID).addEventListener('change', handle_file_select, false);


}